using System;
using System.Collections.Generic;
using System.Text;
using Db4objects.Db4o;

namespace Midas.Core
{
	public sealed class TeamManager: ManagerBase
	{
		private static Dictionary<string, TeamManager> managers;
		static TeamManager()
		{
			managers = new Dictionary<string, TeamManager>();
		}

		public TeamManager():base()
		{
			MidasLogManager.LogEvent("Connection received for TeamManager", 5);
		}

		public bool Login(string username, string password)
		{
			IList<Team> l = client.Query<Team>(delegate(Team t)
			{
				return t.Username .Equals(username) && t.Password.Equals(password);
			});
			if (l.Count != 0)
			{
				IEnumerator<Team> e = l.GetEnumerator();
				e.MoveNext();
				this.loggedInAs = e.Current;
				try
				{
					TeamManager man = managers[loggedInAs.Username];
					if (man.IsReadyForUse)
					{
						throw new MidasAlreadyLoggedInException("You have already logged in");
					}
					managers[loggedInAs.Username] = this;
				}
				catch (KeyNotFoundException)
				{
					managers[loggedInAs.Username] = this;
					return true;
				}
			}
			return false;
		}

		public void SubmitSolution(int problemid, string source)
		{
			throw new Exception("The method or operation is not implemented.");
		}
	}
}
